from xml.etree.ElementTree import ElementTree, dump, fromstring



class RecipeXml(object):
	def __init__(self, xmlstr = ""):
		self.Version      = ''
		self.Destination  = []

		self.__loadfromstring(xmlstr)


	def __load(self, element = None):
		if element != None:
#			try:
			recipe = element.find("Recipe")
			if recipe != None:
				self.Version = recipe.attrib['ApiVer']
				destinations = recipe.findall("Destination")
				for d in destinations:				
					self.Destination.append(TranRecipeDestination(d))

#			except:
#				pass

		else:
			return None

	def __loadfromstring(self, xmlstr = ''):
		if xmlstr != '':
#			try:
			self.__load(fromstring(xmlstr))
#			except:
#				pass


						

class TransRecipeDely(object):
	def __init__(self, element = None):
		self.Host   = ''
		self.User   = ''
		self.Passwd = ''
		self.Path   = ''

		self.__load(element)

	def __load(self, element = None):
		if element != None:
#			try:
			self.Host   = element.find('Host').text
			self.User   = element.find('User').text
			self.Passwd = element.find('Passwd').text
			self.Path   = element.find('Path').text 

#			except:
#				pass


class TransRecipeSegments(object):
	def __init__(self, inpoint='', outpoint='', basename=''):
		self.In  = inpoint
		self.Out = outpoint
		self.Basename = basename


class TransRecipeCarbon(object):
	def __init__(self, element = None):
		self.Ext         = ''
		self.Guid   	 = ''
		self.SegmentType = 'None'
		self.Basename	 = ''
		self.Segments    = []
		self.__load(element)

	def __load(self, element = None):
		if element != None:
#			try:
			self.Guid = element.find('Guid').text
			self.Ext  = element.find('Ext').text

			SegElement = element.find('Segments')
			if SegElement.attrib['Type'] == "None":
				self.SegmentType = "None"
				base    = SegElement.find('Basename')
				if base != None:
                                        self.Basename = base.text
			elif SegElement.attrib['Type'] == "Stich":
				self.SegmentType = "Stich"
				base    = SegElement.find('Basename')
				if base != None:
					self.Basename = base.text	
				segments = SegElement.findall('Segment')
				for s in segments:
					self.Segments.append(TransRecipeSegments(s.attrib['In'], s.attrib['Out']))
			elif SegElement.attrib['Type'] == "Segment":
				self.SegmentType = "Segment"
				segments = SegElement.findall('Segment')
                                for s in segments:
                                        self.Segments.append(TransRecipeSegments(s.attrib['In'], s.attrib['Out'], s.attrib['Basename']))

			
#			except:
#				pass



class TranRecipeDestination(object):
	def __init__(self, element = None):
		self.Name            = ''
		self.Transcode       = False
		self.Engine	     = ''
		self.Dely	     = []
		
		self.__load(element)
	
	def __load(self, element = None):
		if element != None:
			self.Name = element.attrib['Name']
			if element.attrib['Transcode'] == 'True':
				self.Transcode = True
				self.Engine    = element.attrib['Engine']
				if self.Engine == 'Carbon':
					self.transcoder = TransRecipeCarbon(element.find('Carbon'))
			else:
				self.Transcode = False

			dely = element.findall('Dely')
			for d in dely:
				self.Dely.append(TransRecipeDely(d))				

